package com.lzm.ydpt.mvp.presenterimpl;

import com.byt.framlib.basemvp.BasePresenter;
import com.byt.framlib.basemvp.basebean.BaseResponseBean;
import com.byt.framlib.basemvp.basebean.ListPageBean;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.basemvp.exception.ApiException;
import com.byt.framlib.basemvp.listener.OnLoadingErrorListener;
import com.byt.framlib.basemvp.listener.OnLoadingProgressListener;
import com.lzm.ydpt.entity.AddressBook;
import com.lzm.ydpt.module.order.fragment.AddressBookFragment;
import com.lzm.ydpt.mvp.api.ProgressSubscriber;
import com.lzm.ydpt.mvp.contract.AddressBookListContract;
import com.lzm.ydpt.mvp.modelimpl.AddressBookListModelImpl;

/**
 * 类名称：AddressBookListPresenterImpl
 * 类描述：代理实现
 */
public class AddressBookListPresenterImpl extends BasePresenter implements AddressBookListContract.Presenter {

    private AddressBookListContract.View mView;
    private AddressBookListContract.Model mModel;

    public AddressBookListPresenterImpl(AddressBookFragment context) {
        super(context.getActivity());
        this.mView = context;
        this.mModel = new AddressBookListModelImpl();
    }

    @Override
    public void onAddressBookList(FormBodys formBodys) {
        mManager.http(mModel.onAddressBookList(formBodys), getLifecycleProvider(), new ProgressSubscriber<>(mContext, new OnLoadingProgressListener<ListPageBean<AddressBook>>() {
            @Override
            public void onLoadingResult(BaseResponseBean<ListPageBean<AddressBook>> entity) {
                mView.onAddressBookList(entity.getData());
            }
        }, new OnLoadingErrorListener() {
            @Override
            public void onError(ApiException e, String method) {
                mView.showErrorView(e.getDisplayMessage());
            }

            @Override
            public void onErrorResult(BaseResponseBean entity, String method) {
                mView.showErrorView(entity.getMessage());
            }
        }, "onAddressBookList"));
    }

    @Override
    public void onDeleteAddress(FormBodys formBodys, int position) {
        mManager.http(mModel.onDeleteAddress(formBodys), getLifecycleProvider(), new ProgressSubscriber<>(mContext, new OnLoadingProgressListener<Object>() {
            @Override
            public void onLoadingResult(BaseResponseBean<Object> entity) {
                mView.onDeleteAddress(entity.getMessage(), position);
            }
        }, new OnLoadingErrorListener() {
            @Override
            public void onError(ApiException e, String method) {
                mView.showMessage(e.getDisplayMessage(), method);
            }

            @Override
            public void onErrorResult(BaseResponseBean entity, String method) {
                mView.showMessage(entity.getMessage(), method);
            }
        }, "onDeleteAddress"));
    }
}